package com.mgr;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qy.DaoMgr;
import com.qy.bean.Option;
import com.qy.bean.VoucherInfo;
import com.qy.util.StaticData;
import com.qy.util.TimeUtil;

/**
 * 用户代金券管理工具
 * @author zwl
 * 2018年2月1日
 */
public class UserVoucherMgr {
    
    public static VoucherInfo ceateVoucher(int userId,int voucherType){
	VoucherInfo vInfo = new VoucherInfo();
	vInfo.setUserId(userId);
	vInfo.setVoucherType(voucherType);
	vInfo.setStatus(StaticData.VoucherStatus.NOT_PAY.getType());
	vInfo.setBuyUserId(userId);
	vInfo.setCreateDate(TimeUtil.getSysteCurTime());
	vInfo.setOp(Option.Insert);
	DaoMgr.getUserVoucherDao().createVoucherInfo(vInfo);
	
	return vInfo;
    }

    /**
     * 前端获取代金券列表
     * @param userId
     * @return
     */
    public static JSONArray getUserVoucherList(int userId){
	List<VoucherInfo> list = DaoMgr.getUserVoucherDao().getVoucherInfoList(userId);
	JSONArray jsa = new JSONArray();
	JSONObject js = null;
	if(list!=null&&list.size()>0){
	    for (VoucherInfo tmp : list) {
		js = new JSONObject();
		js.put("id", tmp.getId());
		js.put("type", tmp.getVoucherType());
		js.put("status", tmp.getStatus());
		js.put("create", TimeUtil.getDateFormat(tmp.getCreateDate()));
		jsa.add(js);
	    }
	}
	return jsa;
    }
    
    /**
     * 获取用户代金券根据id
     * @param id
     * @return
     */
    public static VoucherInfo getUserVoucher(int id){
	return DaoMgr.getUserVoucherDao().getVoucherInfoById(id);
    }
    
    /**
     * 更新用户代金券
     * @param info
     */
    public static void updateUserVoucher(VoucherInfo info){
	DaoMgr.getUserVoucherDao().updateVoucherInfo(info);
    }
}
